//
//  SGJTaskDetailViewController.m
//  Sewage
//
//  Created by 优聚泰元 on 2017/6/6.
//  Copyright © 2017年 宗庆猛. All rights reserved.
//

#import "SGJTaskDetailViewController.h"
#import "SGJNetworkTools.h"

#import "SGJTaskDetailHeader.h"
#import "SGJTaskDetailCell.h"

#import <YYModel.h>
#import "SGJPoiModel.h"
#import "SGJTempObject.h"

#import "CustomalertViewController.h"
#import "SGJMapTaskViewController.h"

@interface SGJTaskDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (strong,nonatomic) UITableView *tableView;

//@property (strong,nonatomic) NSArray *poiRecordsList;

@property (strong,nonatomic) SGJTempObject *tempObject;

@end

@implementation SGJTaskDetailViewController

-(void)mapItemClick{
    NSLog(@"地图切换");
    SGJMapTaskViewController *mapVc = [[SGJMapTaskViewController alloc] init];
    [self.navigationController pushViewController:mapVc animated:YES];
}

-(void)grabOrderClick{
    NSLog(@"立即抢单");
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"userId"] = [[NSUserDefaults standardUserDefaults] objectForKey:@"userId"];
    
    param[@"taskId"] = self.grabOrderModel.id;
    
    [[SGJNetworkTools sharedNetworkTool] grabOrderWithUrlString:kGrabTaskUrl parameters:param method:@"POST" callBack:^(id responseObject) {
        CustomalertViewController * alertVC =[CustomalertViewController alertControllerWithTitle:nil message:[NSString stringWithFormat:@"您已抢到任务,请在%@前完成",responseObject[@"retData"] ] withSureButtonTitle:@"知道了" withSureCancellTitle:nil preferredStyle:UIAlertControllerStyleAlert handler:^(UIAlertAction *action){
        
        }];
        [self presentViewController:alertVC animated:YES completion:nil];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = self.grabOrderModel.id;
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"地图切换"] style:UIBarButtonItemStylePlain target:self action:@selector(mapItemClick)];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    [self loadData];
    
    [self setupTableView];
    
    UIButton *bottomButton = [UIButton buttonWithType:UIButtonTypeCustom];
    bottomButton.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 55 - 64, [UIScreen mainScreen].bounds.size.width, 55);
    bottomButton.backgroundColor = [UIColor blueColor];
    [bottomButton setTitle:@"立即抢单" forState:UIControlStateNormal];
    [bottomButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    bottomButton.alpha = 0.5;
    [bottomButton addTarget:self action:@selector(grabOrderClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:bottomButton];
}

-(void)setupTableView{
    
    CGRect frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 120);
    UITableView *tableView = [[UITableView alloc] init];
    tableView.frame = CGRectMake(0, 0, frame.size.width, [UIScreen mainScreen].bounds.size.height);
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.delegate = self;
    tableView.dataSource = self;
    SGJTaskDetailHeader *header = [[NSBundle mainBundle] loadNibNamed:@"SGJTaskDetailHeader" owner:nil options:nil].lastObject;
    header.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 145);
    tableView.tableHeaderView = header;
    tableView.tableFooterView = [[UIView alloc] init];
    //[tableView registerClass:[SGJGrabOrderCell class] forCellReuseIdentifier:@"grabOrder"];
    [tableView registerNib:[UINib nibWithNibName:@"SGJTaskDetailCell" bundle:nil] forCellReuseIdentifier:@"TaskDetail"];
    [self.view addSubview:tableView];
    self.tableView = tableView;

}

// 设置去掉navigationBar
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    
    self.navigationController.navigationBarHidden = NO;
    
    //self.navigationController.navigationBar.translucent = YES;
    
    self.navigationController.toolbarHidden = YES;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.tempObject.poiRecords.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SGJTaskDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TaskDetail" forIndexPath:indexPath];
    
    cell.poiModel = self.tempObject.poiRecords[indexPath.row];
    //cell.grabOrderModel = self.grabOrderList[indexPath.row];
    // Configure the cell...
    
   // cell.delegate = self;
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 55;
}

-(void)loadData{
    //
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"id"] = self.grabOrderModel.id;
    param[@"type"] = self.grabOrderModel.type;
    param[@"userId"]= [[NSUserDefaults standardUserDefaults] objectForKey:@"userId"];

    [[SGJNetworkTools sharedNetworkTool] loadTaskDetailByIdWithUrlString:kTaskDetailByIdUrl parameters:param method:@"POST" callBack:^(id responseObject) {
        NSLog(@"任务详情%@",responseObject);

        NSData *jsonData = [responseObject[@"retData"] dataUsingEncoding:NSUTF8StringEncoding];
//
        NSDictionary *tempDict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:NULL];
//
//        self.poiRecordsList = [NSArray yy_modelArrayWithClass:[SGJPoiModel class] json:tempDict[@"poiRecords"]];
        
        SGJTempObject *tempObject = [SGJTempObject yy_modelWithDictionary:tempDict];
        self.tempObject = tempObject;
        //self.poiRecordsList = tempObject.poiRecords;
        [self.tableView reloadData];
    }];
    
}

@end
